<?php

namespace App\Http\Controllers\Api\Push;

use App\Http\Controllers\Api\BaseController;
use App\Models\Orders\OrdersModel;
use App\Repository\Orders\OrdersLogisticsRepository;
use Illuminate\Http\Request;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

// 物流推送过来的信息处理
class ExpressController extends BaseController
{
    private $log;

    public function __construct()
    {
        $this->log = new Logger('Express');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/Express.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
    }

    // 那边推送过来的数据
    public function pushOrderStatus(Request $Request)
    {
        //
        $getData = file_get_contents('php://input');
        $this->log->info('input' . ($getData));
        // return true;
        // return '接受成功';
        // var_dump(1111111111);
        // $getData = 'param=%7B%22status%22%3A%22shutdown%22%2C%22billstatus%22%3A%22check%22%2C%22message%22%3A%22%22%2C%22lastResult%22%3A%7B%22message%22%3A%22ok%22%2C%22nu%22%3A%22773217630145444%22%2C%22ischeck%22%3A%221%22%2C%22condition%22%3A%22F00%22%2C%22com%22%3A%22shentong%22%2C%22status%22%3A%22200%22%2C%22state%22%3A%223%22%2C%22data%22%3A%5B%7B%22time%22%3A%222023-04-21+11%3A28%3A51%22%2C%22ftime%22%3A%222023-04-21+11%3A28%3A51%22%2C%22context%22%3A%22%E5%BF%AB%E4%BB%B6%E5%B7%B2%E8%A2%AB%E9%83%91%E5%B7%9E%E5%A4%A9%E4%BC%A6%E6%B0%B4%E6%99%B6%E5%9F%8E3%E5%8F%B7%E6%A5%BC2%E5%8D%95%E5%85%83%E5%BA%97%E4%BB%A3%E6%94%B6%EF%BC%8C%E8%AF%B7%E5%8F%8A%E6%97%B6%E5%8F%96%E4%BB%B6%E3%80%82%E6%9C%89%E9%97%AE%E9%A2%98%E8%AF%B7%E8%81%94%E7%B3%BB%EF%BC%9A18338460108%EF%BC%8C%E5%A6%82%E6%82%A8%E6%9C%AA%E6%94%B6%E5%88%B0%E6%AD%A4%E5%BF%AB%E9%80%92%EF%BC%8C%E8%AF%B7%E6%8B%A8%E6%89%93%E6%8A%95%E8%AF%89%E7%94%B5%E8%AF%9D%EF%BC%9A037168064664%21+%22%7D%2C%7B%22time%22%3A%222023-04-21+06%3A49%3A10%22%2C%22ftime%22%3A%222023-04-21+06%3A49%3A10%22%2C%22context%22%3A%22%5B%E9%83%91%E5%B7%9E%E5%B8%82%5D%E3%80%90%E6%B2%B3%E5%8D%97%E9%83%91%E5%B7%9E%E7%AC%AC%E5%85%AD%E5%85%AC%E5%8F%B8%E3%80%91037168064664%E7%9A%84%E7%94%B3%E9%80%9A%E5%B0%8F%E5%93%A5%EF%BC%88%E5%BC%A0%E4%B8%9C%E5%9D%A6%2F13213725345%EF%BC%89%E6%AD%A3%E5%9C%A8%E4%B8%BA%E6%82%A8%E6%B4%BE%E9%80%81%EF%BC%88%E5%8F%AF%E6%94%BE%E5%BF%83%E6%8E%A5%E5%90%AC95089%E7%94%B3%E9%80%9A%E4%B8%93%E5%B1%9E%E6%B4%BE%E9%80%81%E5%8F%B7%E7%A0%81%EF%BC%89%2C%E6%8A%95%E8%AF%89%E7%94%B5%E8%AF%9D%EF%BC%9A037168064664%22%7D%2C%7B%22time%22%3A%222023-04-21+06%3A48%3A33%22%2C%22ftime%22%3A%222023-04-21+06%3A48%3A33%22%2C%22context%22%3A%22%5B%E9%83%91%E5%B7%9E%E5%B8%82%5D%E5%BF%AB%E4%BB%B6%E5%B7%B2%E5%88%B0%E8%BE%BE%E3%80%90%E6%B2%B3%E5%8D%97%E9%83%91%E5%B7%9E%E7%AC%AC%E5%85%AD%E5%85%AC%E5%8F%B8%E3%80%91%E5%92%A8%E8%AF%A2%E7%94%B5%E8%AF%9D%EF%BC%9A037168064664%22%7D%2C%7B%22time%22%3A%222023-04-21+06%3A47%3A52%22%2C%22ftime%22%3A%222023-04-21+06%3A47%3A52%22%2C%22context%22%3A%22%5B%E9%83%91%E5%B7%9E%E5%B8%82%5D%E5%BF%AB%E4%BB%B6%E5%B7%B2%E5%88%B0%E8%BE%BE%E3%80%90%E6%B2%B3%E5%8D%97%E9%83%91%E5%B7%9E%E7%AC%AC%E5%85%AD%E5%85%AC%E5%8F%B8%E3%80%91%E5%92%A8%E8%AF%A2%E7%94%B5%E8%AF%9D%EF%BC%9A037168064664%22%7D%2C%7B%22time%22%3A%222023-04-20+20%3A45%3A11%22%2C%22ftime%22%3A%222023-04-20+20%3A45%3A11%22%2C%22context%22%3A%22%5B%E9%83%91%E5%B7%9E%E5%B8%82%5D%E5%BF%AB%E4%BB%B6%E7%A6%BB%E5%BC%80%E3%80%90%E6%B2%B3%E5%8D%97%E9%83%91%E5%B7%9E%E8%BD%AC%E8%BF%90%E4%B8%AD%E5%BF%83%E3%80%91%E5%B7%B2%E5%8F%91%E5%BE%80%E3%80%90%E6%B2%B3%E5%8D%97%E9%83%91%E5%B7%9E%E7%AC%AC%E5%85%AD%E5%85%AC%E5%8F%B8%E3%80%91%22%7D%2C%7B%22time%22%3A%222023-04-20+20%3A33%3A37%22%2C%22ftime%22%3A%222023-04-20+20%3A33%3A37%22%2C%22context%22%3A%22%5B%E9%83%91%E5%B7%9E%E5%B8%82%5D%E5%BF%AB%E4%BB%B6%E7%A6%BB%E5%BC%80%E3%80%90%E6%B2%B3%E5%8D%97%E9%83%91%E5%B7%9E%E8%BD%AC%E8%BF%90%E4%B8%AD%E5%BF%83%E3%80%91%E5%B7%B2%E5%8F%91%E5%BE%80%E3%80%90%E6%B2%B3%E5%8D%97%E9%83%91%E5%B7%9E%E7%AC%AC%E5%85%AD%E5%85%AC%E5%8F%B8%E3%80%91%22%7D%2C%7B%22time%22%3A%222023-04-20+20%3A33%3A20%22%2C%22ftime%22%3A%222023-04-20+20%3A33%3A20%22%2C%22context%22%3A%22%5B%E9%83%91%E5%B7%9E%E5%B8%82%5D%E5%BF%AB%E4%BB%B6%E5%B7%B2%E5%88%B0%E8%BE%BE%E3%80%90%E6%B2%B3%E5%8D%97%E9%83%91%E5%B7%9E%E8%BD%AC%E8%BF%90%E4%B8%AD%E5%BF%83%E3%80%91%22%7D%2C%7B%22time%22%3A%222023-04-20+20%3A02%3A18%22%2C%22ftime%22%3A%222023-04-20+20%3A02%3A18%22%2C%22context%22%3A%22%5B%E9%83%91%E5%B7%9E%E5%B8%82%5D%E5%BF%AB%E4%BB%B6%E5%B7%B2%E5%88%B0%E8%BE%BE%E3%80%90%E6%B2%B3%E5%8D%97%E9%83%91%E5%B7%9E%E8%BD%AC%E8%BF%90%E4%B8%AD%E5%BF%83%E3%80%91%22%7D%2C%7B%22time%22%3A%222023-04-20+19%3A14%3A39%22%2C%22ftime%22%3A%222023-04-20+19%3A14%3A39%22%2C%22context%22%3A%22%5B%E9%87%91%E5%8D%8E%E5%B8%82%5D%E5%BF%AB%E4%BB%B6%E7%A6%BB%E5%BC%80%E3%80%90%E6%B5%99%E6%B1%9F%E9%87%91%E5%8D%8E%E8%BD%AC%E8%BF%90%E4%B8%AD%E5%BF%83%E3%80%91%E5%B7%B2%E5%8F%91%E5%BE%80%E3%80%90%E6%B2%B3%E5%8D%97%E9%83%91%E5%B7%9E%E8%BD%AC%E8%BF%90%E4%B8%AD%E5%BF%83%E3%80%91%22%7D%2C%7B%22time%22%3A%222023-04-20+11%3A14%3A39%22%2C%22ftime%22%3A%222023-04-20+11%3A14%3A39%22%2C%22context%22%3A%22%5B%E9%87%91%E5%8D%8E%E5%B8%82%5D%E5%BF%AB%E4%BB%B6%E5%B7%B2%E5%88%B0%E8%BE%BE%E3%80%90%E6%B5%99%E6%B1%9F%E9%87%91%E5%8D%8E%E8%BD%AC%E8%BF%90%E4%B8%AD%E5%BF%83%E3%80%91%22%7D%2C%7B%22time%22%3A%222023-04-19+23%3A13%3A43%22%2C%22ftime%22%3A%222023-04-19+23%3A13%3A43%22%2C%22context%22%3A%22%5B%E9%87%91%E5%8D%8E%E5%B8%82%5D%E5%BF%AB%E4%BB%B6%E7%A6%BB%E5%BC%80%E3%80%90%E6%B5%99%E6%B1%9F%E4%B9%89%E4%B9%8C%E5%85%AC%E5%8F%B8%E3%80%91%E5%B7%B2%E5%8F%91%E5%BE%80%E3%80%90%E6%B5%99%E6%B1%9F%E9%87%91%E5%8D%8E%E8%BD%AC%E8%BF%90%E4%B8%AD%E5%BF%83%E3%80%91%22%7D%2C%7B%22time%22%3A%222023-04-19+20%3A47%3A12%22%2C%22ftime%22%3A%222023-04-19+20%3A47%3A12%22%2C%22context%22%3A%22%5B%E9%87%91%E5%8D%8E%E5%B8%82%5D%E3%80%90%E6%B5%99%E6%B1%9F%E4%B9%89%E4%B9%8C%E8%90%A5%E9%94%80%E9%83%A82%E3%80%91%284009266666%29%E7%9A%84%E7%9B%9B%E8%BE%89%E4%BB%93%E5%B7%B2%E6%8F%BD%E6%94%B6%22%7D%5D%7D%7D';
        // 数据处理
        if (empty($getData)) {
            return json_encode(['result' => true]);
        }
        if (!empty($getData) && is_string($getData)) {
            $getData = urldecode($getData);
            $getData = str_replace('param=', '', $getData);
            $getData = json_decode($getData, true);
            // var_dump();
        }
        // var_dump($getData);
        if (empty($getData)) {
            return json_encode(['result' => true]);
            return 'error';
        }

        // die();
        $statusDatas = config('express.status');
        //
        // if (!empty($getData['state']) && $getData['state'] == 1) {
        //   $getData['orderState'] = 50;
        // }
        if (empty($getData['lastResult'])) {
            return json_encode(['result' => true]);
        }
        // 将数据格式 修改成固定的数据格式
//        $editData = [
//            'express_status' => !empty($statusDatas[$getData['lastResult']['state']]) ? $getData['lastResult']['state'] : 1000,
//            'express_status_desc' => !empty($statusDatas[$getData['lastResult']['state']]) ? $statusDatas[$getData['lastResult']['state']] : '未知',
//            'express_desc' => !empty($getData['lastResult']['data'][0]['context']) ? $getData['lastResult']['data'][0]['context'] : ''
//        ];
//        // var_dump($editData);
//        $orderId = !empty($getData['lastResult']['nu']) ? $getData['lastResult']['nu'] : $getData['lastResult']['nu'];
//        $where = ['orders.express_number' => $orderId];
//        $model = new MainOrderRepostition();
//        $res = $model->followOrderStatus($where, $editData);

        $this->log->info('快递接口返回参数' . json_encode($getData));
        // 记录物流信息
        $modelLog = new OrdersLogisticsRepository();
        $data = [
            'express_num' => $getData['lastResult']['nu'],
            'express_name' => $getData['lastResult']['com'],  // com
            'text' => json_encode($getData['lastResult']['data'])
        ];
        $modelLog->add($data);
        //            更新订单的物流信息
        $updateOrders = [
            'express_status' => intval($getData['lastResult']['state']),
        ];
        if (!empty($getData['lastResult']['data'])) {
//                取派送电话
            foreach ($getData['lastResult']['data'] as $k => $item) {
                $value = $item['context'];
//                这里记录了派件信息
                $statusCode = $item['statusCode'] ?? -1;
                $statusName = $item['status'] ?? '';
                if ($statusCode == 5 || $statusName == '派件' || strpos($value, '正在派送') !== false) {
                    $updateOrders['express_desc'] = $value;
                    //快递员名字的抓取
//                    $value = str_replace('：', ':', $value);
                    $value = str_replace('派件员', '快递员', $value);
                    $value = str_replace('派送员', '快递员', $value);
                    preg_match("/快递员[：:]+(.+?)，/", $value, $match);
                    if (!empty($match[1])) {
                        $updateOrders['express_desc'] = $match[1];
                    }
                    preg_match('/([0-9]{11})/i', $value, $match);
                    if (!empty($match[1])) {
                        $updateOrders['express_phone'] = $match[1];
                    }
                    break;
                }

            }
        }
        $ModelOrders = new OrdersModel();
        $where = ['express_number' => $getData['lastResult']['nu']];
        $row = $ModelOrders->where($where)->select('id')->first();
        if (!empty($row)) {
            $ModelOrders->where(['id' => $row['id']])->update($updateOrders);
        }
        // 发送短信 的记录
//            dispatch(new GetOrderStatus($where, 'expressStatusSendSms', 10))->onQueue('TaskGetOrderStatusDispose'); // 定时更新状态
        return json_encode(['result' => true]);
    }
}
